package com.hualan.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hualan.mapper.ConsultantMapper;
import com.hualan.mapper.PricingStandardMapper;
import com.hualan.pojo.PageResult;
import com.hualan.pojo.PricingStandard;
import com.hualan.pojo.Result;
import com.hualan.service.PricingStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PricingStandardServiceImpl implements PricingStandardService {
    @Autowired
    PricingStandardMapper pricingStandardMapper;
    @Autowired
    private ConsultantMapper consultantMapper;
    @Override
    public PageResult queryAllPricingStandard(String typeName, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<PricingStandard> pricingStandardList = pricingStandardMapper.queryAllPricingStandard(typeName);
        Page<PricingStandard> p=(Page<PricingStandard>)pricingStandardList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }
    @Override
    public Result addPricingStandard(PricingStandard pricingStandard) {

        try {
            pricingStandardMapper.addPricingStandard(pricingStandard);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        }
    }

    @Override
    public Result updatePricingStandard(PricingStandard pricingStandard) {
        try {
            pricingStandardMapper.updatePricingStandard(pricingStandard);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败");
        }
    }

    @Override
    public Result deletePricingStandard(List<Integer> ids) {
        try {
            pricingStandardMapper.deletePricingStandard(ids);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }

    @Override
    public Integer getConsultantIdByName(String name) {
        return consultantMapper.getConsultantIdByName(name);
    }

    @Override
    public int existsByTypeIdAndPriceAndConsultantId(Integer typeId, Double price, Integer consultantId) {
        return pricingStandardMapper.existsByTypeIdAndPriceAndConsultantId(typeId, price, consultantId);
    }
}
